import {
  CallHandler,
  ExecutionContext,
  HttpCode,
  HttpStatus,
  NestInterceptor,
} from '@nestjs/common';
import { Response } from 'express';
import { map, Observable } from 'rxjs';

export class ResponseInterceptor<T> implements NestInterceptor<T, Response<T>> {
  intercept(
    context: ExecutionContext,
    next: CallHandler<any>,
  ): Observable<any> | Promise<Observable<any>> {
    return next.handle().pipe(map((data: ResponseResult) => data));
  }
}

export class ResponseResult {
  constructor(
    public code: number,
    public message: string,
    public data: any,
  ) {}
  static Success(data: any, message: string = 'success') {
    return new ResponseResult(HttpStatus.OK, message, data);
  }

  static Error(
    code: number = HttpStatus.BAD_REQUEST,
    message: string,
    data?: any,
  ) {
    return new ResponseResult(code, message, data);
  }
}
